package login;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class UsuariosServlet
 */

public class UsuariosServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public UsuariosServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * Este Servlet Permite ingresar los datos a la lista de usuarios ademas de modificarlas y eliminarlas
	 * 
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String accion = request.getParameter("accion");
		List<VOLogin> lista = (List<VOLogin>) request.getSession().getAttribute("ListaUsuarios");
		VOLogin vo = new VOLogin();
		vo.setUsuario(request.getParameter("Usuario"));
		vo.setPassword(request.getParameter("Password"));
		vo.setNombre(request.getParameter("Nombres"));
		vo.setEdad(Integer.parseInt(request.getParameter("Edad")));
		if (accion.equals("guardar")) {
			lista.add(vo);
		} else if (accion.equals("eliminar")) {
			for (VOLogin obj : lista) {
				if (obj.getUsuario().equals(vo.getUsuario())) {
					lista.remove(obj);
					break;
				}
			}
		} else if (accion.equals("actualizar")) {
			for (VOLogin obj : lista) {
				if (obj.getUsuario().equals(vo.getUsuario())) {
					obj.setPassword(vo.getPassword());
					obj.setEdad(vo.getEdad());
					obj.setNombre(vo.getNombre());
					break;
				}

			}
		}
		response.sendRedirect("Usuarios.jsp");
	}
}
